package com.study.servlet;

import com.study.dao.AdminInfoDAO;
import com.study.dao.impl.AdminInfoDAOImpl;
import com.study.dto.RegisterDto;
import com.study.util.MD5;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.text.SimpleDateFormat;

/**
 * @PackgeName: com.study.servlet
 * @ClassName: RegisterServlet
 * @Author: 严志祥
 * Date: 2022-05-14 22:23
 * project name: web_project_medal
 * @Version:
 * @Description:
 */
@WebServlet("/register.let")
public class RegisterServlet extends HttpServlet {
    private AdminInfoDAO dao = new AdminInfoDAOImpl();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    private AdminInfoDAO d = new AdminInfoDAOImpl();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) {
        try {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        String adminName = req.getParameter("adminName");
        String adminPassword = req.getParameter("adminPassword");
        String time = req.getParameter("adminTime");
        if (adminName == null || "".equals(adminName)) {
            String str = "账号不能为空";
            req.setAttribute("name", str);
            req.getRequestDispatcher("/register.jsp").forward(req, resp);
        }else if(adminPassword == null || "".equals(adminPassword)){
            String str = "密码不能为空";
            req.setAttribute("pwd", str);
            req.getRequestDispatcher("/register.jsp").forward(req, resp);
        }else if(d.selectByAdminName(adminName)){
                    String str = "用户名已存在";
                    req.setAttribute("username",str);
                    req.getRequestDispatcher("/register.jsp").forward(req, resp);
                }if(adminName.length()<3){
                String str = "用户名长度必须大于三位";
                req.setAttribute("usernameLength",str);
                req.getRequestDispatcher("/register.jsp").forward(req, resp);
            }if(adminPassword.length()<5){
                String str = "密码长度必须大于五位";
                req.setAttribute("pwdLength",str);
                req.getRequestDispatcher("/register.jsp").forward(req, resp);
            }else {
                    RegisterDto dto = new RegisterDto();
                    try {
                        dto.setAdminName(adminName);
                        dto.setAdminPassword(MD5.encode(adminPassword));
                        dto.setAdminTime(sdf.parse(time));
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    try {
                        dao.insert(dto);
                        resp.getWriter().print("{\"ok\":true}");
                        req.getRequestDispatcher("/login.html").forward(req, resp);
                    } catch (SQLException e) {
                        throw new ServletException("管理员编号重复", e);
                    }
                }
        } catch (Exception e) {
        }
    }
}
